using System;
using System.Linq;
using System.Windows.Forms;
using Autofac;
using ComputerVision.BacChiuLuaForm;
using ComputerVision.CanBoQuanLyForm;
using ComputerVision.CapQuanLyHanhChinhForm;
using ComputerVision.CoQuanCapVanBanThamDuyetForm;
using ComputerVision.CoQuanQuanLyPCCCForm;
using ComputerVision.Data;
using ComputerVision.DoiForm;
using ComputerVision.Forms;
using ComputerVision.HangSanXuatForm;
using ComputerVision.HinhThucDauTuForm;
using ComputerVision.LoaiCoSoForm;
using ComputerVision.LoaiHinhHoatDongForm;
using ComputerVision.LoaiNguonNuocForm;
using ComputerVision.LucLuongThucTapForm;
using ComputerVision.NganhForm;
using ComputerVision.NhomHanhViForm;
using ComputerVision.NhomPhuongTienForm;
using ComputerVision.PhanLoaiChiTietNguonNuocForm;
using ComputerVision.PhanLoaiChiTietPhuongTienForm;
using ComputerVision.PhieuDieuTraCoSoForm;
using ComputerVision.PhongForm;
using ComputerVision.PhuongTienThucTapForm;
using ComputerVision.QuanHuyenForm;
using ComputerVision.Role;
using ComputerVision.Services;
using ComputerVision.ThanhPhanKinhTeForm;
using ComputerVision.VanBanThamDuyetForm;

namespace ComputerVision
{
    public partial class frmMain : Form
    {
        private IService<PhieuDieuTraCoSo> _phieuDieuTraCoSo;
        private IService<ThamDuyetPCCC> _thamDuyetPcCc;
        private IService<HangMuc> _hangMuc;
        private IService<PhuongTienCoSo> _phuongTienCoSo;
        private IService<NguonNuoc> _nguonNuoc;
        private IService<TuyenTruyenHuanLuyen> _tuyenTruyen;
        private IService<KiemTraAnToan> _kiemTra;
        private IService<PhuongAnChuaChay> _phuongan;
        private IService<XuLyViPham> _xuLyViPham;

        public frmMain()
        {
            InitializeComponent();
        }

        #region Closed

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            DeleteTempRecords();

            foreach (Form openForm in Application.OpenForms)
            {
                openForm.Close();
            }
            Application.Exit();

        }

        #endregion

        private void mnuInfo_Click(object sender, EventArgs e)
        {
            var frm = new frmInformation();
            frm.ShowDialog();
        }

        private void CloseMainForm(object sender, FormClosedEventArgs e)
        {
            DeleteTempRecords();

            while (Application.OpenForms.Count > 0)
            {
                Application.OpenForms[0].Close();
            }
        }

        private void mnuItemLogin_Click(object sender, EventArgs e)
        {
            var frm = new frmLogin();
            frm.Show();
            Hide();
        }

        private void mnuItemChangePassword_Click(object sender, EventArgs e)
        {
            var frm = new frmChangePassword();
            frm.ShowDialog();
        }

        private void mnuItemRoles_Click(object sender, EventArgs e)
        {
            var frm = new frmRoleList {MdiParent = this, WindowState = FormWindowState.Maximized};
            frm.Show();
        }

        private void mnuItemListUser_Click(object sender, EventArgs e)
        {
            var frm = new frmUserInRole {MdiParent = this, WindowState = FormWindowState.Maximized};
            frm.Show();
        }

        private void mnuItemQuanHuyen_Click(object sender, EventArgs e)
        {
            var frm = new frmQuanHuyenList {MdiParent = this, WindowState = FormWindowState.Maximized};
            frm.Show();
        }

        private void mnuItemHinhThucDauTu_Click(object sender, EventArgs e)
        {
            var frm = new frmHinhThucDauTuList {MdiParent = this, WindowState = FormWindowState.Maximized};
            frm.Show();
        }

        private void mnuItemLoaiCoSo_Click(object sender, EventArgs e)
        {
            var frm = new frmLoaiCoSoList {MdiParent = this, WindowState = FormWindowState.Maximized};
            frm.Show();
        }

        private void mnuItemLoaiHinhHoatDong_Click(object sender, EventArgs e)
        {
            var frm = new frmLoaiHinhHoatDongList {MdiParent = this, WindowState = FormWindowState.Maximized};
            frm.Show();
        }

        private void mnuItemPhong_Click(object sender, EventArgs e)
        {
            var frm = new frmPhongList {MdiParent = this, WindowState = FormWindowState.Maximized};
            frm.Show();
        }

        private void mnuItemDonVi_Click(object sender, EventArgs e)
        {
            var frm = new frmDoiList {MdiParent = this, WindowState = FormWindowState.Maximized};
            frm.Show();
        }

        private void mnuItemCanBoQuanLy_Click(object sender, EventArgs e)
        {
            var frm = new frmCanBoQuanLyList {MdiParent = this, WindowState = FormWindowState.Maximized};
            frm.Show();
        }

        private void mnuItemNganh_Click(object sender, EventArgs e)
        {
            var frm = new frmNganhList {MdiParent = this, WindowState = FormWindowState.Maximized};
            frm.Show();
        }

        private void mnuItemPhieuDieuTraCoSo_Click(object sender, EventArgs e)
        {
            var frm = new frmPhieuDieuTraCoSoList {MdiParent = this, WindowState = FormWindowState.Maximized};
            frm.Show();
        }

        private void mnuItemCapQuanLyHanhChinh_Click(object sender, EventArgs e)
        {
            var frm = new frmCapQuanLyHanhChinhList {MdiParent = this, WindowState = FormWindowState.Maximized};
            frm.Show();
        }

        private void mnuThanhPhanKinhTe_Click(object sender, EventArgs e)
        {
            var frm = new frmThanhPhanKinhTeList {MdiParent = this, WindowState = FormWindowState.Maximized};
            frm.Show();
        }

        private void mnuItemVanBanThamDuyet_Click(object sender, EventArgs e)
        {
            var frm = new frmVanBanThamDuyetList {MdiParent = this, WindowState = FormWindowState.Maximized};
            frm.Show();
        }

        private void mnuItemCoQuanCapVanBanThamDuyet_Click(object sender, EventArgs e)
        {
            var frm = new frmCoQuanCapVanBanThamDuyetList {MdiParent = this, WindowState = FormWindowState.Maximized};
            frm.Show();
        }

        private void mnuItemCoQuanQuanLyPCCC_Click(object sender, EventArgs e)
        {
            var frm = new frmCoQuanQuanLyPCCCList {MdiParent = this, WindowState = FormWindowState.Maximized};
            frm.Show();
        }

        private void mnuItemBacChiuLua_Click(object sender, EventArgs e)
        {
            var frm = new frmBacChiuLuaList {MdiParent = this, WindowState = FormWindowState.Maximized};
            frm.Show();
        }

        private void mnuItemHangSanXuat_Click(object sender, EventArgs e)
        {
            var frm = new frmHangSanXuatList {MdiParent = this, WindowState = FormWindowState.Maximized};
            frm.Show();
        }

        private void mnuItemNhomPhuongTien_Click(object sender, EventArgs e)
        {
            var frm = new frmNhomPhuongTienList {MdiParent = this, WindowState = FormWindowState.Maximized};
            frm.Show();
        }

        private void mnuItemPhanLoaiChiTietPhuongTien_Click(object sender, EventArgs e)
        {
            var frm = new frmPhanLoaiChiTietPhuongTienList {MdiParent = this, WindowState = FormWindowState.Maximized};
            frm.Show();
        }

        private void mnuItemLoaiNguonNuoc_Click(object sender, EventArgs e)
        {
            var frm = new frmLoaiNguonNuocList {MdiParent = this, WindowState = FormWindowState.Maximized};
            frm.Show();
        }

        private void mnuItemPhanLoaiChiTietNguonNuoc_Click(object sender, EventArgs e)
        {
            var frm = new frmPhanLoaiChiTietNguonNuocList {MdiParent = this, WindowState = FormWindowState.Maximized};
            frm.Show();
        }

        private void DeleteTempRecords()
        {
            _phieuDieuTraCoSo = Program.Container.Resolve<IService<PhieuDieuTraCoSo>>();
            _thamDuyetPcCc = Program.Container.Resolve<IService<ThamDuyetPCCC>>();
            _hangMuc = Program.Container.Resolve<IService<HangMuc>>();
            _phuongTienCoSo = Program.Container.Resolve<IService<PhuongTienCoSo>>();
            _nguonNuoc = Program.Container.Resolve<IService<NguonNuoc>>();
            _tuyenTruyen = Program.Container.Resolve<IService<TuyenTruyenHuanLuyen>>();
            _kiemTra = Program.Container.Resolve<IService<KiemTraAnToan>>();
            _phuongan = Program.Container.Resolve<IService<PhuongAnChuaChay>>();
            _xuLyViPham = Program.Container.Resolve<IService<XuLyViPham>>();

            var items = _phieuDieuTraCoSo.ToList().ToList().Where(p => p.Status.Equals(false));
            foreach (var item in items)
            {
                var thamDuyet = _thamDuyetPcCc.ToList().ToList().Where(p => p.PhieuDieuTraCoSoId.Equals(item.Id));
                foreach (var thamDuyetPccc in thamDuyet)
                {
                    _thamDuyetPcCc.Delete(thamDuyetPccc);
                }

                var hangMuc = _hangMuc.ToList().ToList().Where(p => p.PhieuDieuTraCoSo_Id.Equals(item.Id));
                foreach (var hm in hangMuc)
                {
                    _hangMuc.Delete(hm);
                }

                var phuongTien = _phuongTienCoSo.ToList().ToList().Where(p => p.PhieuDieuTraCoSoId.Equals(item.Id));
                foreach (var pt in phuongTien)
                {
                    _phuongTienCoSo.Delete(pt);
                }

                var nguonNuoc = _nguonNuoc.ToList().ToList().Where(p => p.PhieuDieuTraCoSoesId.Equals(item.Id));
                foreach (var nn in nguonNuoc)
                {
                    _nguonNuoc.Delete(nn);
                }

                var tuyenTruyen = _tuyenTruyen.ToList().ToList().Where(p => p.PhieuDieuTraCoSoId.Equals(item.Id));
                foreach (var tt in tuyenTruyen)
                {
                    _tuyenTruyen.Delete(tt);
                }

                var kiemTra = _kiemTra.ToList().ToList().Where(p => p.PhieuDieuTraCoSoesId.Equals(item.Id));
                foreach (var tt in kiemTra)
                {
                    _kiemTra.Delete(tt);
                }

                var phuongan = _phuongan.ToList().ToList().Where(p => p.PhieuDieuTraCoSoId.Equals(item.Id));
                foreach (PhuongAnChuaChay tt in phuongan)
                {
                    _phuongan.Delete(tt);
                }

                var xuLyViPham = _xuLyViPham.ToList().ToList().Where(p => p.PhieuDieuTraCoSoesId.Equals(item.Id));
                foreach (XuLyViPham tt in xuLyViPham)
                {
                    _xuLyViPham.Delete(tt);
                }

                _phieuDieuTraCoSo.Delete(item);
            }
        }

        private void mnuItemNhomHanhVi_Click(object sender, EventArgs e)
        {
            var frm = new frmNhomHanhViList {MdiParent = this, WindowState = FormWindowState.Maximized};
            frm.Show();
        }

        private void mnuItemLucLuongThucTap_Click(object sender, EventArgs e)
        {
            var frm = new frmLucLuongThucTapList {MdiParent = this, WindowState = FormWindowState.Maximized};
            frm.Show();
        }

        private void mnuItemPhuongTienThucTap_Click(object sender, EventArgs e)
        {
            var frm = new frmPhuongTienThucTapList {MdiParent = this, WindowState = FormWindowState.Maximized};
            frm.Show();
        }
    }
}